#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>

#include "list.h"					  /* use the SimCList library */
#include "utils.h"
#define TRUE 1
#define FALSE 0

int compint(const void *a,const void *b) {
/*	printf("%8d vs %8d\n",*(int *)a , *(int *)b); */
	return( (*(int *)a < *(int *)b)?FALSE:TRUE );
}

int main(int argc,char **argv)
{
	list *mylist;	
	int userval;
	int i,*rnum;
	listIter *iter;
	listNode *node;
	struct timeval tmv;

	mylist = listCreate();
	mylist->free = free;

	userval = atol(argv[1]);

	for (i = 0; i<userval ; i++){
		rnum = calloc(1,sizeof(int));
		*rnum = randi(10000);
		listAddNodeHead(mylist, rnum);
		}

	for (i = 0; i<10; i++){
		rnum = calloc(1,sizeof(int));
		*rnum = 1000;
		listAddNodeHead(mylist, rnum);
		}


	list_qsort(mylist,compint);

	tic(&tmv);
	for (i = 0; i<5; i++){
		rnum = calloc(1,sizeof(int));
		*rnum = randi(10000);
		list_binsert(mylist, rnum,compint);
		}
	toc(&tmv);

	printf("\nThe list now holds %u elements.\n", listLength(mylist));

        iter=listGetIterator(mylist,AL_START_HEAD);
	i = 0;
	while ((node = listNext(iter)) != NULL) {
		i++;
		;;
	}
	listReleaseIterator(iter);
	printf("listLength %d len is %8d", listLength(mylist),i);

	printf("\n");

	listRelease(mylist);

	return 0;
}
